package com.ldy.job.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.ldy.job.ReportUtilsApplication;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.ldy.job.listener.NoModelDataListener.*;
import static com.ldy.job.utils.ExcelUtils.*;
import static com.ldy.job.utils.PPTUtils.changPPT;
import static com.ldy.job.utils.WriteDoc.writeDoc;

@Slf4j
public class NoModel3DataListener extends AnalysisEventListener<Map<Integer, String>> {
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;
    private static final String[] INDEX_STR = new String[8];
    private static final String[] INDEX2_STR = new String[7];
    private static final String[] INDEX3_STR = new String[10];
    private Map<String, String> tempData = new LinkedHashMap<>();
    private Map<String, String> tempData2 = new LinkedHashMap<>();
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    static {
        INDEX_STR[0] = "31_1";
        INDEX_STR[1] = "31_2";
        INDEX_STR[2] = "31_11";
        INDEX_STR[3] = "31_12";
        INDEX_STR[4] = "31_8";
        INDEX_STR[5] = "31_9";
        INDEX_STR[6] = "31_5";
        INDEX_STR[7] = "31_6";

        INDEX2_STR[0] = "31_3";
        INDEX2_STR[1] = "31_11";
        INDEX2_STR[2] = "31_12";
        INDEX2_STR[3] = "31_8";
        INDEX2_STR[4] = "31_9";
        INDEX2_STR[5] = "31_5";
        INDEX2_STR[6] = "31_6";

        INDEX3_STR[0] = "3_0";
        INDEX3_STR[1] = "3_3";
        INDEX3_STR[2] = "4_0";
        INDEX3_STR[3] = "4_3";
        INDEX3_STR[4] = "5_0";
        INDEX3_STR[5] = "5_3";
        INDEX3_STR[6] = "6_0";
        INDEX3_STR[7] = "6_3";
        INDEX3_STR[8] = "7_0";
        INDEX3_STR[9] = "7_3";
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
//        log.info("解析行数:{}", context.readRowHolder().getRowIndex());
//        log.info("解析到一条数据:{}", data);
        cachedDataList.add(data);
        RESSTR_YEWUYUNXINGFENXI = doCheck(data, context, RESSTR_YEWUYUNXINGFENXI, INDEX_STR, tempData, "");
        RESSTR_YEWUYUNXINGFENXI = doCheck(data, context, RESSTR_YEWUYUNXINGFENXI, INDEX2_STR, tempData2, "");
        RESSTR_YEWUYUNXINGFENXI = doCheck(data, context, RESSTR_YEWUYUNXINGFENXI, INDEX3_STR, tempData2, "p");
        if (cachedDataList.size() >= BATCH_COUNT) {
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    private String doCheck(Map<Integer, String> data, AnalysisContext context, String resStr, String[] indexStr, Map<String, String> tempData, String flag) {
        for (String string : indexStr) {
            String[] index = string.split("_");
            if (context.readRowHolder().getRowIndex().equals(Integer.parseInt(index[0]))) {
                String s = data.get(Integer.parseInt(index[1]));
                if (!s.contains("%")) {
                    s = amountTool(s);
                }
                if (tempData == this.tempData) {
                    resStr = resStr.replaceFirst("#X#", s);
                }
                tempData.put(string + flag, s);
            }
        }
        return resStr;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info(tempData.toString());
        tempData.put("y", getLastYear());
        tempData.put("m", getLastMonth());
        tempData.putAll(doCount());
        writeDoc(tempData);
        tempData2.putAll(doSum());
        changPPT(ReportUtilsApplication.DOC_PATH + "\\生成\\“单一窗口”及信保易线上签单情况月报(" + getLastMonth() + "月).pptx", ReportUtilsApplication.DOC_PATH + "\\生成\\“单一窗口”及信保易线上签单情况月报(" + getLastMonth() + "月).pptx", tempData2);
    }

}
